<template>
  <div>
      <!-- 面包屑 -->
     <el-breadcrumb separator-class="el-icon-arrow-right">
         <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
         <el-breadcrumb-item>毕业生就业情况</el-breadcrumb-item>
      </el-breadcrumb>

     <!-- 头部区域 -->
      <el-card class="box-card">
                <el-row :gutter="20">
                    <el-col :span="7">
               <el-input placeholder="请输入内容"  class="input-with-select" v-model="queryInfo.query" clearable @clear="getUserList">
                   <el-button slot="append" icon="el-icon-search" @click="getUserListt"></el-button>
               </el-input>
            </el-col>
        </el-row>
          <!-- 搜索与添加区域 -->


        <el-table :data="userlist" border stripe>
            <el-table-column type="index"></el-table-column>
            <el-table-column label="学号" prop="xuehao"></el-table-column>
            <el-table-column label="姓名" prop="name"></el-table-column>
            <el-table-column label="专业" prop="zhuanye"></el-table-column>
             <el-table-column label="是否就业" prop="jiuye"></el-table-column>
            <el-table-column label="工作岗位" prop="gangwei"></el-table-column>
            <el-table-column label="工作是否匹配专业" prop="pipei"></el-table-column>
            <el-table-column label="薪资" prop="money"></el-table-column>
             <el-table-column label="就业满意" prop="manyi"></el-table-column>
             <el-table-column label="详细说明" prop="xiangxi"></el-table-column>


            <el-table-column label="操作" width="180px">
                <template v-slot="scope">
                     <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)">修改</el-button>
                 </template>
            </el-table-column>


            <el-table-column label="操作" width="180px" v-if="this.tokenn == 'admin'">
                <template v-slot="scope">
                     <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserByid(scope.row.id)">删除</el-button>
                 </template>
            </el-table-column>

        </el-table>

         <!-- 分页区域 -->
        <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="queryInfo.pageindex"
                  :page-sizes="[1, 2, 5, 10]"
                  :page-size="queryInfo.pagesize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
        </el-pagination>
      </el-card>


      <!-- 修改用户的对话框 -->
      <el-dialog
        title="修改用户" :visible.sync="editDialogVisble" width="30%" @close="addDialogClosed">
        <el-form :model="editForm" :rules="addFormRules" ref="addFormref" label-width="70px" >

           <el-form-item label="是否就业" >
               <el-input v-model="editForm.jiuye"></el-input>
           </el-form-item>

            <el-form-item label="工作岗位">
               <el-input v-model="editForm.gangwei"></el-input>
           </el-form-item>

          <el-form-item label="工作是否匹配专业">
               <el-input v-model="editForm.pipei"></el-input>
           </el-form-item>


             <el-form-item label="薪资" >
               <el-input v-model="editForm.money"></el-input>
           </el-form-item>
            <el-form-item label="就业满意" >
               <el-input v-model="editForm.manyi"></el-input>
           </el-form-item>
            <el-form-item label="详细说明" >
               <el-input v-model="editForm.xiangxi"></el-input>
           </el-form-item>

        </el-form>

        <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisble = false">取 消</el-button>
        <el-button type="primary" @click="editUserInfo()">确 定</el-button>
        </span>
      </el-dialog>


  </div>
</template>

<script>
export default {
    name:'',
    data() {
            //验证邮箱的规则
            var checkEmail =(rule,value,cb)=>{
                const regEail=/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
                if(regEail.test(value)){
                    //合法的邮箱
                    return cb()
                }
                cb(new Error('请输入合法的邮箱'))
            }
            //验证手机号的规则
            /*var checkMobile =(rule,value,cb)=>{
                //验证手机号的正则表达式
                const regMobile = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])d{8}$/
                   if(regMobile.test(value)){
                    //合法的手机号
                    return cb()
                }
                cb(new Error('手机号不合法'))
            }*/
        return {

            //获取用户列表
            queryInfo:{
                query:'',
                //当前的页数
                pageindex:1,
                pagesize:5
            },
            userlist:[],
            total:0,

            //添加用户对话框的显示隐藏
            addDialogVisible:false,

            //添加修改用户对话框的显示隐藏
            editDialogVisble:false,

            //查询到的用户信息对象
            editForm:{
                jiuye:'',
                gangwei:'',
                money:'',
                manyi:'',
                xiangxi:''
            },

            //分配角色对话框的显示和隐藏
            setRoleDialogVisible:false,

            //添加用户的表单数据
            addForm:{
                xuehao:'',
                name:'',
                zhuanye:'',
                ipone:'',
                biye:'',
                pipei:''
            },

            //添加表单验证规则对象
            addFormRules:{
                username:[
                     { required: true, message: '请输入登陆名称', trigger: 'blur' },
                     { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password:[
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 15, message: '长度在 6 到 10 个字符', trigger: 'blur' }
                ],
                 email:[
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    {validator:checkEmail,trigger:'blur'}
                ],
                /* mobile:[
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    {validator:checkMobile,trigger:'blur'}
                ]*/
            },

            //需要被分配角色的用户信息
            userInfo:'',

            //所以角色的列表
            rolesList:[],

            //选中的角色id值
            selectedRoleId:''
        }
    },
    created() {
        this.getUserList()
    },
    methods: {
         getUserList(){
          this.$http.get('/getlist').then(res=>{
             this.userlist = res.data
            this.total = res.data.total
            console.log(res);
          })

        },
        //监听pagesize改变的事件
        handleSizeChange(newSize){
            this.queryInfo.pagesize=newSize
            this.getUserList()
        },
        //监听页码值 改变的事件
        handleCurrentChange(newPage){
            this.queryInfo.pageindex=newPage
            this.getUserList()
        },

        getUserListt(){
         this.$http.post('/getuser',this.queryInfo.query).then(res=>{
               var ttt= Array.from(Object.values(res),x=>x)
                this.userlist = ttt
                console.log(res);
                console.log(ttt);
                console.log(this.userlist);
         })
        },

        //监听添加用户的对话框的关闭事件
        addDialogClosed(){
            this.$refs.addFormref.resetFields()
        },
        //添加新用户
        addUser(){
                this.$http.post('/api/add/news',this.addForm)
                this.$message.success('添加用户成功')
                //隐藏添加用户的对话框
                this.addDialogVisible = false
                //重新获取用户的列表
                this.getUserList()
            
        },
        //根据id删除信息
         removeUserByid(id){
         this.$http.post('/deleteuser',id)
         .then(res=>{
              this.userlist = res.data
         })
         console.log(id);
                this.getUserList()
        },
        
        //对话框关闭后情况对话框里的内容
        setRoleDialogClosed(){
            this.selectedRoleId=''
            this.userInfo=''
        },
        
        //展示修改内容的对话框
        showEditDialog(id){
             this.$http.post('/getuser',id).then(res=>{
             this.editForm = res.data
             console.log(this.editForm);
          })
            this.editDialogVisble=true
        },
        //修改用户信息并提交
        editUserInfo(){
            this.$http.post("/updateuser",this.editForm).then(function(res){
                this.userlist=res.data
                this.getUserList()
            })        
             this.editDialogVisble=false

                //刷新数据列表
            
        },

        
        getUserListt(){
         this.$http.post('/getuserr',this.queryInfo.query).then(res=>{
               var ttt= Array.from(Object.values(res),x=>x)
                this.userlist = ttt
                console.log(res);
                console.log(ttt);
                console.log(this.userlist);
         })
        },
    }
}
</script>

<style>
.el-table{
    margin-top: 15px;
}
</style>